﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
using Mailer.Model;
using Mailer.Model.Types;
using NHibernate.Criterion;

namespace Mailer.Security
{
	public class RoleProvider : System.Web.Security.RoleProvider
	{
		public RoleProvider()
		{
			Logger.Debug(typeof(RoleProvider), "In constructor.");
		}

		public override void AddUsersToRoles(string[] usernames, string[] roleNames)
		{
			Logger.Debug(typeof(RoleProvider), "AddUsersToRoles.");

			ISession sess = SessionUtilities.BeginSession();
			if (sess != null)
			{

				using (ITransaction tt = sess.BeginTransaction())
				{
					foreach (string username in usernames)
					{
						User dbuser = sess.CreateCriteria(typeof(Mailer.Model.Types.User))
							.Add(Expression.Eq("Username", username))
							.UniqueResult<User>();

						if (dbuser != null)
						{
							foreach (string rolename in roleNames)
							{
								Role r = sess.Get<Role>(rolename);
								if (r != null)
									if (!dbuser.Roles.Contains(r))
										dbuser.Roles.Add(r);
							}

							sess.Save(dbuser);
						}
					}

					tt.Commit();
				}
				SessionUtilities.EndSession(sess);
			}
		}

		public override string ApplicationName
		{
			get
			{
				return "Mailer";
			}
			set
			{
			}
		}

		public override void CreateRole(string roleName)
		{
			Logger.Debug(typeof(RoleProvider), "CreateRole.");
			throw new NotImplementedException();
		}

		public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
		{
			Logger.Debug(typeof(RoleProvider), "DeleteRole.");
			throw new NotImplementedException();
		}

		public override string[] FindUsersInRole(string roleName, string usernameToMatch)
		{
			Logger.Debug(typeof(RoleProvider), "FindUsersInRole.");
			throw new NotImplementedException();
		}

		public override string[] GetAllRoles()
		{
			Logger.Debug(typeof(RoleProvider), "GetAllRoles.");
			throw new NotImplementedException();
		}

		public override string[] GetRolesForUser(string username)
		{
			Logger.Debug(typeof(RoleProvider), "GetRolesForUser.");
			throw new NotImplementedException();
		}

		public override string[] GetUsersInRole(string roleName)
		{
			Logger.Debug(typeof(RoleProvider), "GetUsersInRole.");
			// throw new NotImplementedException();

			List<string> ret = new List<string>();


			ISession sess = SessionUtilities.BeginSession();
			if (sess != null)
			{

				IList<User> users = sess.CreateQuery("select u from User as u inner join u.Roles as r where r.Name = :role")
					.SetParameter("role", roleName)
					.List<User>();

				foreach (User u in users)
					ret.Add(u.Username);
				 
				SessionUtilities.EndSession(sess);
			}



			//	ret.Add("

			return ret.ToArray<string>();
		}

		public override bool IsUserInRole(string username, string roleName)
		{
			Logger.Debug(typeof(RoleProvider), "IsUserInRole.");
			throw new NotImplementedException();
		}

		public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
		{
			Logger.Debug(typeof(RoleProvider), "RemoveUsersFromRoles.");
			throw new NotImplementedException();
		}

		public override bool RoleExists(string roleName)
		{
			Logger.Debug(typeof(RoleProvider), "RoleExists.");
			throw new NotImplementedException();
		}
	}
}
